package com.cj.pj.sys.common.shiro.config;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    @Bean
    public SecurityManager securityManager(Realm realm){
        DefaultWebSecurityManager sManager= new DefaultWebSecurityManager();
        sManager.setRealm(realm);
        return  sManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //指定登录url
        shiroFilterFactoryBean.setLoginUrl("/doLoginUI");
        //这个map用来存放对于进行安全控制的页面设计，key是路径，value是属性，属性有两个：anon允许匿名访问静态资源，authc需要经过认证之后才可以访问
        LinkedHashMap<String,String> linkedHashMap=new LinkedHashMap<>();
        linkedHashMap.put("/bower_components/**","anon");
        linkedHashMap.put("/build/**","anon");
        linkedHashMap.put("/dist/**","anon");
        linkedHashMap.put("/plugins/**","anon");

        linkedHashMap.put("/user/login","anon");

        linkedHashMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(linkedHashMap);
        System.out.println("拦截器注入");
        return shiroFilterFactoryBean;
    }
}
